package postgres

import (
	"context"
	"gorm.io/gorm"
	"manager/model"
	"manager/model/entity"
)

var Repair = &repair{}

type repair struct{}

// Create 创建
func (a *repair) Create(ctx context.Context, m *entity.Repair) (int64, error) {
	err := GetDB(ctx).Create(m).Error
	return m.Id, err
}

// Find 查找详情
func (a *repair) Find(ctx context.Context, id int64) (*entity.Repair, error) {
	e := &entity.Repair{}
	err := GetDB(ctx).Model(&entity.Repair{}).First(&e, id).Error
	return e, err
}

// Update 更新
func (a *repair) Update(ctx context.Context, id int64, dict map[string]interface{}) error {
	return GetDB(ctx).Model(&entity.Repair{}).Where("id = ?", id).Updates(dict).Error
}

// Delete 删除
func (a *repair) Delete(ctx context.Context, id int64) error {
	return GetDB(ctx).Delete(&entity.Repair{}, id).Error
}

// List 列表查询
func (a *repair) List(ctx context.Context, in *model.RepairListRequest) (int, []*entity.Repair, error) {
	var (
		q       = GetDB(ctx).Model(&entity.Repair{})
		err     error
		total   int64
		repairs []*entity.Repair
	)

	if in.Reportman != nil {
		q = q.Where("reportman = ?", in.Reportman)
	}

	if in.EbikeId != nil {
		q = q.Where("ebike_id = ?", in.EbikeId)
	}

	if in.FaultId != nil {
		q = q.Where("fault_id = ?", in.FaultId)
	}

	if in.FaultTypeId != nil {
		q = q.Where("fault_type_id = ?", in.FaultTypeId)
	}

	if in.FaultTypeName != nil {
		q = q.Where("fault_type_name = ?", in.FaultTypeName)
	}

	if in.EbikeTypeId != nil {
		q = q.Where("ebike_type_id = ?", in.EbikeTypeId)
	}

	if in.EbikeTypeName != nil {
		q = q.Where("ebike_type_name = ?", in.EbikeTypeName)
	}

	if in.EbikeTypeImage != nil {
		q = q.Where("ebike_type_image = ?", in.EbikeTypeImage)
	}

	if in.Other != nil {
		q = q.Where("other = ?", in.Other)
	}

	if in.Type != nil {
		q = q.Where("type = ?", in.Type)
	}

	if in.Coordinate != nil {
		q = q.Where("coordinate = ?", in.Coordinate)
	}

	if in.Photo != nil {
		q = q.Where("photo = ?", in.Photo)
	}

	if in.OrderNo != nil {
		q = q.Where("order_no = ?", in.OrderNo)
	}

	if in.Status != nil {
		q = q.Where("status = ?", in.Status)
	}

	if in.Repairman != nil {
		q = q.Where("repairman = ?", in.Repairman)
	}

	if in.Price != nil {
		q = q.Where("price = ?", in.Price)
	}

	if in.StoreId != nil {
		q = q.Where("store_id = ?", in.StoreId)
	}

	if in.Desc != nil {
		q = q.Where("desc = ?", in.Desc)
	}

	if in.RepairAt != nil {
		q = q.Where("repair_at = ?", in.RepairAt)
	}

	if in.UpdatedAt != nil {
		q = q.Where("updated_at = ?", in.UpdatedAt)
	}

	if len(in.OrderNos) > 0 {
		q = q.Where("order_no in ?", in.OrderNos)
	}

	if in.StartTime != nil && in.EndTime != nil {
		q = q.Where("created_at between ? and ?", in.StartTime, in.EndTime)
	}

	if len(in.TimeZone) == 2 {
		q = q.Where("created_at between ? and ?", in.TimeZone[0], in.TimeZone[1])
	}

	if err = q.Count(&total).Error; err != nil {
		return 0, nil, err
	}
	if err = q.Order("created_at desc").Limit(in.Size).Offset((in.Index - 1) * in.Size).Find(&repairs).Error; err != nil {
		return 0, nil, err
	}
	return int(total), repairs, nil
}

// ExecTransaction db事务执行
func (a *repair) ExecTransaction(ctx context.Context, callback func(ctx context.Context) error) error {
	return GetDB(ctx).Transaction(func(tx *gorm.DB) error {
		ctx = context.WithValue(ctx, DBCONTEXTKEY, tx)
		return callback(ctx)
	})
}
